package com.example.weather_news.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.chaychan.viewlib.PowerfulEditText;
import com.example.weather_news.R;
import com.example.weather_news.adapter.Adapter_SearchNews;
import com.example.weather_news.http.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SearchNewsActivity extends AppCompatActivity {

    private ImageView iv_back;
    private PowerfulEditText et_searchNews;
    private TextView tv_search;
    private ListView listView_search;
    private JSONArray arrays;
    private Adapter_SearchNews adapter;
    private ProgressBar pb_loading;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_news);
        initView();

        adapter = new Adapter_SearchNews(SearchNewsActivity.this);
        listView_search.setAdapter(adapter);

        tv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });

        listView_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(SearchNewsActivity.this, DetailNewsActivity.class);
                Bundle bundle = new Bundle();
                try {
                    JSONObject jo = arrays.getJSONObject(position);
                    String url = jo.getString("weburl");
                    bundle.putString("url", url);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        et_searchNews = (PowerfulEditText) findViewById(R.id.et_searchNews);
        tv_search = (TextView) findViewById(R.id.tv_search);
        listView_search = (ListView) findViewById(R.id.listView_search);
        pb_loading= (ProgressBar) findViewById(R.id.pb_loading);
    }

    private void submit() {
        arrays = null;
        // validate
        String searchNews = et_searchNews.getText().toString().trim();
        if (TextUtils.isEmpty(searchNews)) {
            Toast.makeText(this, "搜索内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        // TODO validate success, do something
        pb_loading.setVisibility(View.VISIBLE);
        HttpRequest.requestSearchNews(searchNews, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.getInt("status") != 0) {
                        Toast.makeText(SearchNewsActivity.this, "搜索失败，请重试",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    String result = jsonObject.getString("result");
                    JSONObject jo = new JSONObject(result);
                    String list = jo.getString("list");
                    arrays = new JSONArray(list);
                    Log.e("arrays", arrays.length() + "");
                    adapter.setData(arrays);
                    pb_loading.setVisibility(View.GONE);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, null);

        //隐藏键盘
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(tv_search.getWindowToken(), 0);
    }
}
